package org.summerframework.component.mybites.config;

import org.summerframework.component.mybites.redis.LoggingRedisCache;
import lombok.Getter;
import lombok.Setter;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @author 石超
 * @version v1.0.0
 */
@Configuration
@AutoConfigureBefore({TransactionManagementConfiguration.class, MybatisAutoConfiguration.class})
@EnableConfigurationProperties(MybatisConfiguration.MyBatisRedisProperties.class)
public class MybatisConfiguration {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private MyBatisRedisProperties myBatisRedisProperties;

    @PostConstruct
    public void setLoggingRedisCache() {
        LoggingRedisCache.setRedisId(myBatisRedisProperties.getId());
        LoggingRedisCache.setRedisTemplate(redisTemplate);
    }

    @Getter
    @Setter
    @ConfigurationProperties("spring.redis.mybatis")
    public static class MyBatisRedisProperties {
        private String id;

        public String getId() {
            return id;
        }
    }
}
